package com.ssm.car.server.controller.capture;


import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.car.dao.model.Capture;
import com.ssm.car.rpc.api.capture.CaptureService;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 抓拍记录表管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/capture")
public class CaptureController{


    /**
     * 进入抓拍记录表添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("capture:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/capture/add";
    }


    /**
     * 进入抓拍记录表编辑页面
     * @param model 返回抓拍记录表的容器
     * @param id 抓拍记录表id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("capture:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",captureService.selectById(id));
        return "manager/capture/edit";
    }


    /**
     * 抓拍记录表添加
     * @param capture 带id的抓拍记录表对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("capture:add")
    @AdminControllerLog(description = "添加抓拍记录表" )
    public Tip save(Capture capture)  {

        try {
            captureService.insert(capture);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改抓拍记录表
     * @param capture 带id的抓拍记录表对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("capture:edit")
    @AdminControllerLog(description = "修改抓拍记录表" )
    public Tip update(Capture capture) {

        try {
            captureService.updateById(capture);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除抓拍记录表
     * @param id 抓拍记录表id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("capture:delete")
    @AdminControllerLog(description = "删除抓拍记录表" )
    public Tip delete(@RequestParam String id) {

        try {
            captureService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 抓拍记录表id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("capture:delete")
    @AdminControllerLog(description = "批量删除抓拍记录表" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            captureService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找抓拍记录表
     * @param id 抓拍记录表id
     * @return 抓拍记录表对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("capture:list")
    public Capture find(@RequestParam String id) {

        return captureService.selectById(id);
    }


    /**
     * 获取抓拍记录表列表 获取全部 不分页
     * @param request 请求参数
     * @return 抓拍记录表列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("capture:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取抓拍记录表列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 抓拍记录表列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("capture:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {

        modelMap.put("pageInfo",captureService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/capture/list";
    }


    //---------------------------- property -------------------------------

    @Resource
    private CaptureService captureService;

}
